java连接git进行操作(Jgit) |
您所在的位置:网站首页 › java git操作 工具类 › java连接git进行操作(Jgit) |
背景
其实也没有什么背景,就是想用java操作git上的项目来实现一些小功能,略过略过。 文章目录 背景二、使用步骤开发环境框架环境1.添加依赖2.编写demo遇到的问题问题总结 二、使用步骤我是参考 java 使用jgit 操作 git 来搭建的,最终遇到了一些报错,所以博客终归只是一个参考,需要自己去上手实现,好了,废话不多说,看下面步骤 开发环境 maven3.6+git任意版本idea 2018.x+ 框架环境 springboot 2.x+jgit 5.9.0.202009080501-rjava-gitlab-api 4.1.1 1.添加依赖我这里是依赖的eclipse提供的jgit org.eclipse.jgit org.eclipse.jgit ${jgit.version} import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import warnings warnings.filterwarnings('ignore') import ssl ssl._create_default_https_context = ssl._create_unverified_context 2.编写demo 写一个git的工具类 public class GitUtils { public static CredentialsProvider connect(String email, String password, String apiToken){ UsernamePasswordCredentialsProvider provider = new UsernamePasswordCredentialsProvider(email, password); return provider; } @SneakyThrows public static boolean cloneProject(String gitUrl){ String property = System.getProperty("user.home"); String baseName = FilenameUtils.getBaseName(gitUrl); StringBuilder build = new StringBuilder(property).append(File.separator).append(baseName); if (Files.exists(Paths.get(build.toString()))) { Files.delete(Paths.get(build.toString())); } Files.createDirectories(Paths.get(build.toString())); //git提供的api来clone项目 Git call = Git.cloneRepository().setURI(gitUrl) .setCloneAllBranches(true) .setDirectory(new File(build.toString())) .call(); List branchs = call.branchList().call(); //打印它的所有分支 System.out.println(branchs); return false; } public static void main(String[] args) { cloneProject("[email protected]:choviwu/qywechat.git"); } }写到这里您可能就开始跑main方法了,结果发现报错了,这都是在预料之中的,好了,看下面↓ 遇到的问题很多博客都没有依赖 org.eclipse.jgit.ssh.jsch依赖,这是因为我搭建的时候遇到了一个报错,并google了一下,得出了答案 Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: [email protected]:choviwu/qywechat.git: remote hung up unexpectedly at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:222) at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:302) ...这是eclipse公司的bug: 原地址这事实上已经在5.8+修复了, 然后我们去他老人家给的链接去看看 在接jgit的时候搜了很多资料(百度),发现博客上的资料千篇一律,大家都文(抄)采(来)飞(抄)扬(去),没有可以参考的资料,看源码太费时间,因此去google了一下,才看到ecplise官网提到了这个fixed,因此,不要太过于相信网上的博客,博客上大家写(拷)的也许是几年前的产物,如果用新的版本遇到的坑大家都得自己去规避,最有效的办法就是自己去翻源码(当然每个人能力不一),就比如jgit的源码用到了不少设计模式,不得不说ecplise公司对代码的要求还是挺高,也可能是国外程序员加班少,没有“福报”…… 好了,这里就踩坑踩到这里,后面有的再加上! 项目地址: 项目地址 本篇为原创,转载须注明来源,谢谢 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |